﻿using System;
using org.se.fs.presenter.IView.Template;
using org.se.fs.presenter.Library;

namespace org.se.fs.presenter.View
{
    public class BranchSubMenu : Template.SubMenu<Domain.Branch>, ISubMenus<Domain.Branch>
    {
        private readonly IView.IBranchSubMenu _view;
        private readonly ITask.IBranch _task;

        public BranchSubMenu(IView.IBranchSubMenu view) : this(view, new Task.Context.Branch()) { }

        public BranchSubMenu(IView.IBranchSubMenu view, ITask.IBranch task)
        {
            _view = view;
            _task = task;
        }

        public void Initialize()
        {
            _task.OnDelete += TaskOnInsert;
            _task.OnFetch += TaskOnFetch;
            _task.OnFetchLists += TaskOnFetchLists;
            _task.OnInsert += TaskOnInsert;
            _task.OnToggle += TaskOnToggle;
            _task.OnUpdate += TaskOnUpdate;

            _view.SetLabel(Uid.Equals(0) ? @"Create New Branch" : @"Modify Branch");
            _view.ButtonAddVisible(Uid.Equals(0));
            _view.ButtonUpdateVisible(!Uid.Equals(0));
        }

        public void TaskOnFetch(object source, FormEvent e)
        {
        }

        public void TaskOnFetchLists(object source, FormEvent e)
        {
        }

        public void TaskOnInsert(object source, FormEvent e)
        {
            _view.ButtonAddEnable(false);
            TriggerModify(new FormEvent(e.IsSuccess) { Exception = e.Exception, Value = e.Value });
        }

        public void TaskOnUpdate(object source, FormEvent e)
        {
            _view.ButtonUpdateEnable(false);
            TriggerModify(new FormEvent(e.IsSuccess) {Exception = e.Exception, Value = e.Value});
        }

        public void TaskOnDelete(object source, FormEvent e)
        {
        }

        public void TaskOnToggle(object source, FormEvent e)
        {
        }

        public void SetData(Domain.Branch data)
        {
            Uid = data.Id;
            _view.FullName = data.Name;
            _view.Code = data.Code;
            _view.Address = data.Address;
            _view.Fax = data.FaxNum;
            _view.Email = data.Email;
            _view.Mobile = data.MobileNum;
            _view.Tel = data.TelNum;
        }

        public void Add()
        {
            if (!Validate())
                return;

            _view.ButtonAddEnable(false); 
            _task.Insert(GetData());
        }

        public void Modify()
        {
            if(!Validate())
                return;

            _view.ButtonUpdateEnable(false);
            _task.Update(GetData());
        }

        public bool Validate()
        {
            if (string.IsNullOrEmpty(_view.FullName.Trim()))
            {
                TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty name.") });
                _view.FocusName();
                return false;
            }

            if (string.IsNullOrEmpty(_view.Code.Trim()))
            {
                TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty code.") });
                _view.FocusCode();
                return false;
            }

            if (string.IsNullOrEmpty(_view.Address.Trim()))
            {
                TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty address.") });
                _view.FocusAddress();
                return false;
            }

            TriggerValidate(new FormEvent(true));
            return true;
        }

        public Domain.Branch GetData()
        {
            return new Domain.Branch
                       {
                           Id = Uid,
                           Name = _view.FullName,
                           Code = _view.Code,
                           Address = _view.Address,
                           FaxNum = _view.Fax,
                           Email = _view.Email,
                           MobileNum = _view.Mobile,
                           TelNum = _view.Tel,
                           User = Config.User
                       };
        }
    }
}
